package com.snow.useviewdemo.function;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.snow.common.tool.utils.ToastUtils;
import com.snow.rpermission.RequestPermissions;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.databinding.ActAndroidNativeShareBinding;
import com.snow.useviewdemo.html.WebViewPDFAct;
import com.snow.useviewdemo.util.FileUtils;
import com.snow.useviewdemo.util.OnAntiDoubleClickListener;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-12-27
 * desc   :系统分享，调用其他应用打开文件,调用系统文件管理器
 */
public class AndroidNativeShareActivity extends BaseActivity<ActAndroidNativeShareBinding> {
    @Override
    protected int getLayoutId() {
        return R.layout.act_android_native_share;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        //请求存储权限
        RequestPermissions.with(this)
                .permissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE})
                .onGranted(strings -> {

                })
                .onDenied(strings -> {

                })
                .start();

        mBinding.btn01.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                if (!TextUtils.isEmpty(selectFilePath)) {
                    FileUtils.init().openFileThirdApp(AndroidNativeShareActivity.this, selectFilePath);
                } else {
                    ToastUtils.getInstance().showToast("先选择文件");
                }
            }
        });
        mBinding.btn02.setOnClickListener(new OnAntiDoubleClickListener() {

            @Override
            public void onAntiDoubleClick(View v) {
                FileUtils.init().shareFileForWhichApp(AndroidNativeShareActivity.this, selectFilePath, FileUtils.WEICHAT);
            }
        });
        mBinding.btn03.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                FileUtils.init().shareFileForWhichApp(AndroidNativeShareActivity.this, selectFilePath, FileUtils.WEICHAT_CIRCE);
//                ShareFileUtils.shareImageToWeChatFriend(AndroidNativeShareActivity.this, selectFilePath);
            }
        });
        mBinding.btn04.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                FileUtils.init().shareFileForWhichApp(AndroidNativeShareActivity.this, selectFilePath, FileUtils.QQ);
            }
        });
        mBinding.btn06.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                FileUtils.init().shareFileForWhichApp(AndroidNativeShareActivity.this, selectFilePath, FileUtils.QQ_CIRCE);
//                ShareFileUtils.shareImageToQZone(AndroidNativeShareActivity.this, selectFilePath);
            }
        });
        mBinding.btn05.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                openPhoneFileManager();
            }
        });
        mBinding.btn07.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                if (TextUtils.isEmpty(selectFilePath)) {
                    ToastUtils.getInstance().showToast("请先选择文件");
                    return;
                }
                Intent intent = new Intent(AndroidNativeShareActivity.this, WebViewPDFAct.class);
                intent.putExtra("selectFilePath", selectFilePath);
                startActivity(intent);
            }
        });
    }

    String selectFilePath;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
            selectFilePath = FileUtils.init().getPathFromUri(this, uri);
        }
    }


    //打开手机文件管理器
    private void openPhoneFileManager() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
//        intent.setType("application/pdf");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 1);

        //intent.setType("*/*");//打开任意文件
        //intent.setType("image/*");//选择图片
        //intent.setType("application/pdf");//打开PDF文件
        //intent.setType("application/vnd.android.package-archive");//打开apk文件
        //intent.setType("application/vnd.ms-powerpoint");//打开PPT文件
        //intent.setType("application/vnd.ms-excel");//打开Excel文件
        //intent.setType("application/msword");//打开Word文件
        //intent.setType("text/plain");//打开文本文件

        //intent.setType("audio/*"); //选择音频
        //intent.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
        //intent.setType("video/*;image/*");//同时选择视频和图片
    }
}
